const {ccclass, property} = cc._decorator;
import Global = require("./GlobalEvents");

@ccclass
export default class GameEvents extends cc.Component {  
    
    @property(cc.Label)
    scoreLbl: cc.Label = null;

    @property({
        type: cc.AudioClip
    })
    BGAudio = null;

    // LIFE-CYCLE CALLBACKS:
    btnClick(event, customEventData){
        //console.log("event=",event.type," data=",customEventData);
        if(customEventData==0){
            //cc.log("event=",event.type," data=",customEventData);
            //this.shadowSprite.getComponent(cc.Animation).play();
            this.node.runAction(cc.fadeOut(0.7));
            let callbak = function (){
                cc.director.loadScene("GamePlay");
            };
            this.scheduleOnce(callbak, 1.0);            
            //this.node.runAction(cc.fadeIn(1.0));
        }
        else if(customEventData==1){
            if(Global.isSound==1){
                Global.isSound = 0;                
            }
            else{
                Global.isSound = 1;
            }

        }
        else if(customEventData==2){
            if(Global.isMusic==1){
                //cc.log("Music pause");
                Global.isMusic = 0;
                cc.audioEngine.pauseMusic();                
            }
            else{
                //cc.log("Music play");
                Global.isMusic = 1;
                cc.audioEngine.resumeMusic();                
            }
        }
        else if(customEventData==3){
            this.node.runAction(cc.fadeOut(0.7));
            let callbak = function (){
                cc.director.loadScene("GameOver");
            };
            this.scheduleOnce(callbak, 0.7); 
        }
        else{

        }
       };

    onLoad () {
        
        let self = this;     

        //判断是否播放音乐
        if(Global.isMusic==1)
        {
            if(Global.GameState=="load")
            {
                //self.getComponent(cc.AudioSource).play();
                cc.audioEngine.playMusic(self.BGAudio, true);
            }            
        }
        else{}

        // 加载背景 独立的SpriteFrame
        let bgrand = Math.ceil(Math.random() * 6) - 1;        
        Global.BGpic = bgrand;         
        
        cc.director.preloadScene("GamePlay", function () 
        {
            //cc.log("Next scene preloaded");
        });
        
        cc.director.preloadScene("GameOver", function () 
        {
            //cc.log("Next scene preloaded");
        });

        if(!cc.sys.localStorage.getItem("BestScore"))
        {
            Global.BestScore = 0;
            self.scoreLbl.string = "0";
        }
        else
        {            
            let bs = cc.sys.localStorage.getItem("BestScore");
            Global.BestScore = Number(bs);
            self.scoreLbl.string = bs;
        }

        //开启右上角的分享
        window["wx"].showShareMenu();        
        //监听右上角的分享调用
        cc.loader.loadRes("Icon_128",function(err,data){            
            window["wx"].onShareAppMessage(function(res){                
                return {                    
                    title: "猴哥有难，快来帮忙！",				
                    imageUrl: data.url,				
                    success(res){                        
                        console.log("转发成功")},                        
                    fail(res){
                        console.log("转发失败")}
                }		
            })	
        });                 
    }

    start () 
    {        
        Global.GameState=="Init";        
    }

    // update (dt) {}
}
